#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class GraduationInfoProvider : CY.GFive.Core.Providers.DALProvider.IGraduationInfoProvider
	{
		#region Sql
		private static readonly string SqlSelectGraduationInfo = "SELECT [Id], [StdCode],[EmployDate],[EntCode],[EntName],[ProDate],[AddDate],[EmployCate],[AdderCode],[ProtcolCode] FROM [GraduationInfo] WHERE  [Id]=@Id";

		private static readonly string SqlInsertGraduationInfo = "INSERT INTO [GraduationInfo]([StdCode],[EmployDate],[EntCode],[EntName],[ProDate],[AddDate],[EmployCate],[AdderCode],[ProtcolCode]) values(@StdCode,@EmployDate,@EntCode,@EntName,@ProDate,@AddDate,@EmployCate,@AdderCode,@ProtcolCode);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateGraduationInfo = "UPDATE [GraduationInfo] SET [StdCode] = @StdCode,[EmployDate] = @EmployDate,[EntCode] = @EntCode,[EntName] = @EntName,[ProDate] = @ProDate,[AddDate] = @AddDate,[EmployCate] = @EmployCate,[AdderCode] = @AdderCode,[ProtcolCode] = @ProtcolCode WHERE  [Id]=@Id";

		private static readonly string SqlDeleteGraduationInfo = "DELETE [GraduationInfo] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllGraduationInfo = "SELECT [Id], [StdCode],[EmployDate],[EntCode],[EntName],[ProDate],[AddDate],[EmployCate],[AdderCode],[ProtcolCode] FROM [GraduationInfo]";
		#endregion

		#region IGraduationInfoProvider Members
		public Core.Business.GraduationInfo Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectGraduationInfo);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.GraduationInfo graduationInfo = new Core.Business.GraduationInfo();

				if (!reader.IsDBNull(0)) graduationInfo.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) graduationInfo.StdCode = reader.GetString(1);
				if (!reader.IsDBNull(2)) graduationInfo.EmployDate = reader.GetDateTime(2);
				if (!reader.IsDBNull(3)) graduationInfo.EntCode = reader.GetString(3);
				if (!reader.IsDBNull(4)) graduationInfo.EntName = reader.GetString(4);
				if (!reader.IsDBNull(5)) graduationInfo.ProDate = reader.GetDateTime(5);
				if (!reader.IsDBNull(6)) graduationInfo.AddDate = reader.GetDateTime(6);
				if (!reader.IsDBNull(7)) graduationInfo.EmployCate = reader.GetString(7);
				if (!reader.IsDBNull(8)) graduationInfo.AdderCode = reader.GetString(8);
				if (!reader.IsDBNull(9)) graduationInfo.ProtcolCode = reader.GetString(9);

				return graduationInfo;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.GraduationInfo graduationInfo)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@StdCode", SqlDbType.NVarChar, graduationInfo.StdCode);
			sql.AddParameter("@EmployDate", SqlDbType.DateTime, graduationInfo.EmployDate);
			sql.AddParameter("@EntCode", SqlDbType.NVarChar, graduationInfo.EntCode);
			sql.AddParameter("@EntName", SqlDbType.NVarChar, graduationInfo.EntName);
			sql.AddParameter("@ProDate", SqlDbType.DateTime, graduationInfo.ProDate);
			sql.AddParameter("@AddDate", SqlDbType.DateTime, graduationInfo.AddDate);
			sql.AddParameter("@EmployCate", SqlDbType.NVarChar, graduationInfo.EmployCate);
			sql.AddParameter("@AdderCode", SqlDbType.NVarChar, graduationInfo.AdderCode);
			sql.AddParameter("@ProtcolCode", SqlDbType.NVarChar, graduationInfo.ProtcolCode);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertGraduationInfo);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) graduationInfo.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.GraduationInfo graduationInfo)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@StdCode", SqlDbType.NVarChar, graduationInfo.StdCode);
			sql.AddParameter("@EmployDate", SqlDbType.DateTime, graduationInfo.EmployDate);
			sql.AddParameter("@EntCode", SqlDbType.NVarChar, graduationInfo.EntCode);
			sql.AddParameter("@EntName", SqlDbType.NVarChar, graduationInfo.EntName);
			sql.AddParameter("@ProDate", SqlDbType.DateTime, graduationInfo.ProDate);
			sql.AddParameter("@AddDate", SqlDbType.DateTime, graduationInfo.AddDate);
			sql.AddParameter("@EmployCate", SqlDbType.NVarChar, graduationInfo.EmployCate);
			sql.AddParameter("@AdderCode", SqlDbType.NVarChar, graduationInfo.AdderCode);
			sql.AddParameter("@ProtcolCode", SqlDbType.NVarChar, graduationInfo.ProtcolCode);
			sql.AddParameter("@Id", SqlDbType.Int, graduationInfo.Id);
			sql.ExecuteSql(SqlUpdateGraduationInfo);
		}

		public void Delete(Core.Business.GraduationInfo graduationInfo)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, graduationInfo.Id);
			sql.ExecuteSql(SqlDeleteGraduationInfo);
		}

		public IList<Core.Business.GraduationInfo> GetAllGraduationInfo()
		{
			IList<Core.Business.GraduationInfo> graduationInfolist = new List<Core.Business.GraduationInfo>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllGraduationInfo);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.GraduationInfo graduationInfo = new Core.Business.GraduationInfo();

					if (!reader.IsDBNull(0)) graduationInfo.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) graduationInfo.StdCode = reader.GetString(1);
					if (!reader.IsDBNull(2)) graduationInfo.EmployDate = reader.GetDateTime(2);
					if (!reader.IsDBNull(3)) graduationInfo.EntCode = reader.GetString(3);
					if (!reader.IsDBNull(4)) graduationInfo.EntName = reader.GetString(4);
					if (!reader.IsDBNull(5)) graduationInfo.ProDate = reader.GetDateTime(5);
					if (!reader.IsDBNull(6)) graduationInfo.AddDate = reader.GetDateTime(6);
					if (!reader.IsDBNull(7)) graduationInfo.EmployCate = reader.GetString(7);
					if (!reader.IsDBNull(8)) graduationInfo.AdderCode = reader.GetString(8);
					if (!reader.IsDBNull(9)) graduationInfo.ProtcolCode = reader.GetString(9);

					graduationInfo.MarkOld();
					graduationInfolist.Add(graduationInfo);
				}
				reader.Close();
			}
			return graduationInfolist;
		}
		#endregion
	}
}
